package com.sky.d_thread.sync;

/**
 * 对象锁
 * @Author:hjt
 */
public class AccouttingSyncObject implements Runnable {
	static AccouttingSyncObject instance = new AccouttingSyncObject();
	static int i = 0;

	@Override
	public void run() {
		for (int j = 0;j < 1000000; j ++){
			//同步对象锁，线程执行需要获取对象锁才会执行，保证执行结果数据达到预期
			synchronized (instance) {
				i ++;
			}
		}
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		Thread t1 = new Thread(instance);
		Thread t2 = new Thread(instance);
		
		t1.start();
		t2.start();
		
		//join 等待线程执行完毕，打印两次线程一起的结果
		t1.join();
		t2.join();
		
		System.out.println("执行完毕,i=" + i);
	}
}
